package com.mrtan.service.impl;

import com.mrtan.bean.Model;
import com.mrtan.bean.Product;
import com.mrtan.bean.SupplierProduct;
import com.mrtan.mapper.ModelMapper;
import com.mrtan.mapper.ProductMapper;
import com.mrtan.mapper.SupplierProductMapper;
import com.mrtan.service.ProductService;
import com.mrtan.vo.ProductCountVo;
import com.mrtan.vo.ProductWhereVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 产品
 * @author 李聪
 * @version 2023/12/8
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ModelMapper modelMapper;
    @Resource
    private SupplierProductMapper supplierProductMapper;
    @Override
    public List<Product> findAll(ProductWhereVO productWhereVO) {
        return productMapper.selectAll(productWhereVO);
    }

    /**
     * 添加商品
     * @param productWhereVO
     * @return int
     */
    @Override
    public int addProduct(ProductWhereVO productWhereVO) {
        //查看类型有没有
        Model model = modelMapper.selectByPrimaryKey(productWhereVO.getModel());
        if(model==null){
            return -1;
        }
        // 查看有没有该产品
        Product product =productMapper.select(productWhereVO);
        if(product!=null){
            return -1;
        }

        int i = productMapper.addProduct(productWhereVO);
        return i;
    }

    /**
     * 删除产品
     * @param id
     * @return int
     */
    @Override
    public int deleteProduct(Integer id) {
        //先看仓库有无该产品
        Product product =productMapper.selectByPrimaryKey(id);
        if(product==null){
            return -1;
        }
        int i = productMapper.deleteByPrimaryKey(id);
        return i;
    }
    @Override
    public List<Object[]> getCount(String model) {
        List<ProductCountVo> count = productMapper.getCount(model);
        List<Object[]> list=new ArrayList<>();
        String name[]=new String[count.size()];
        Integer quantity[]=new Integer[count.size()];
        String type[]=new String[count.size()];
        for (int i = 0; i < count.size(); i++) {
            name[i]=count.get(i).getName();
            quantity[i]=count.get(i).getQuantity();
            type[i]=count.get(i).getModel();
        }
        list.add(name);
        list.add(quantity);
        list.add(type);
        return list;
    }
}
